﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.UI.Blog.NodeTypes;
using Medianamik.Core.Sugar;
using Medianamik.Core;
using Medianamik.Globalization;
using Medianamik.UI.Controls;

namespace Medianamik.UI.Blog
{
    public partial class editBlogEntry : BaseModuleEditionPage<BlogEntry>
    {
        public void Page_Load(object sender, EventArgs e)
        {
            InitializeControls();
        }

        private void InitializeControls()
        {
            btnSaveAndPublish.Visible = CurrentNode.NodeType.IsVersioned;

            btnRevert.Visible = CurrentNode.NodeType.IsVersioned &&
                                IsEdition && CurrentNode.IsDraft
                                && !Revision.HasValue
                                && CurrentNode.DefaultInstance.Revisions.Length > 0;
            btnRevert.Attributes.Add("onclick", "return confirm('" + Translator.TranslateJS("Medianamik_ContentManagement", "ConfirmRevert") + "');");
        }

        protected void btnSaveAndPublish_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var node = NodeManager.SaveNode(MedianamikFormTemplate.GetFormValue(CurrentNode));
                NodeManager.PublishNode(node.ID, CurrentEditionCulture, DateTime.Now, default(DateTime?));
                Response.Redirect(ReturnURL);
            }
        }

        protected void btnRevert_Click(object sender, EventArgs e)
        {
            NodeManager.RevertNode(NodeId, CurrentEditionCulture);
            Response.Redirect(ReturnURL);
        }

        protected override string ReturnURL
        {
            get { return "blogList.aspx?parentid=" + CurrentNode.ParentID; }
        }

        public override MedianamikFormTemplate MedianamikFormTemplate
        {
            get { return Fields; }
        }

        public override Guid? TypeId
        {
            get
            {
                return BlogEntry.TypeId;
            }
        }

        public override UpdatePanel FieldsPanel
        {
            get { return AllFieldsPanel; }
        }

        protected override Guid? ParentNodeId
        {
            get
            {
                return base.ParentNodeId ?? BaseBlog.ModuleParentId;
            }
        }

        public override LinkButton SaveButton
        {
            get { return btnSave; }
        }
    }
}
